package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.view.View;
import android.widget.EditText;

import com.android.databinding.library.baseAdapters.BR;
import com.solidfire.gson.internal.LinkedTreeMap;

import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.PopupActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.WaitActivityResult;

/**
 * Created by zxh on 16/12/20.
 */

public class ExpertCommentModel extends ObservableWithGoBackHandler implements WaitActivityResult {
    private int uid;
    private int consultId;
    private String avatar;
    private String job;
    private String nickName;
    private String realName;
    private String company;
    private float rating;
    private boolean attentioned;
    private String content;

    public int getConsultId() {
        return consultId;
    }

    public void setConsultId(int consultId) {
        this.consultId = consultId;
    }

    public int getUid() {
        return uid;
    }

    @Bindable
    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @Bindable
    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    @Bindable
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Bindable
    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
        notifyPropertyChanged(BR.job);
    }

    @Bindable
    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
        notifyPropertyChanged(BR.realName);
    }

    @Bindable
    public float getRating() {
        return rating;
    }

    public void setRating(float rating) {
        this.rating = Math.round(rating);
        notifyPropertyChanged(BR.rating);
    }

    @Bindable
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Bindable
    public boolean isAttentioned() {
        return attentioned;
    }

    public void setAttentioned(boolean attentioned) {
        this.attentioned = attentioned;
        notifyPropertyChanged(BR.attentioned);
    }

    public ExpertCommentModel(Context context, int expertId) {
        super(context);
        super.getHandler().setTitle("我的咨询");
        uid = expertId;
        load();
    }

    public void onClickAttention(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (!attentioned) {
                    if (WebApi.addAttention(getContext(), getUid())) {
                        setAttentioned(false);
                    }
                } else {
                    if (WebApi.unAttention(getContext(), getUid())) {
                        setAttentioned(true);
                    }
                }
            }
        }).start();
    }

    /**
     * 点击投诉按钮
     *
     * @param view
     */
    public void onClickComplain(View view) {
        Activity activity = (Activity) getContext();
        ExpertComplainModel model = new ExpertComplainModel(activity, uid);
        model.setAvatar(getAvatar());
        model.setJob(getJob());
        model.setConsultId(getConsultId());
        model.setNickName(getNickName());
        model.setRealName(getRealName());
        model.setCompany(getCompany());
        CommonFragment fragment = new CommonFragment();
        fragment.setDataModel(model);
        fragment.setLayoutId(R.layout.f_complain);
        fragment.setVariableId(BR.complain);
        HomeActivity.switchFragment(activity, fragment);
    }

    /**
     * 专家评论
     *
     * @param view
     */
    public void onClickSubmit(View view) {
        if (rating <= 0) {
            showToast("请选择评分");
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (WebApi.doJudjer(getContext(), getUid(), getConsultId(), getContent(), rating)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DialogModel model = new DialogModel(getContext());
                            PopupActivity.startActivityForResult((Activity) getContext(),
                                    R.layout.f_advice_comment_commit, HomeActivity.REQUEST_DIALOGTIP,
                                    BR.dialog, model);
                        }
                    });
                }
            }
        }).start();
    }

    private void load() {
        LoadingDialog.getInstance(getContext()).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
//                avatar:头像,nickname:昵称,company：公司,job:标签,咨询Id:consultid,专家id:expertid,gzflag
                LinkedTreeMap result = WebApi.expertComplain(getContext(), getConsultId());
                if (result != null) {
                    uid = Utils.getIntValue(result.get("expertid"));
                    setAvatar((String) result.get("avatar"));
                    setNickName((String) result.get("nickname"));
                    setCompany((String) result.get("company"));
                    setJob((String) result.get("job"));
                    setConsultId(Utils.getIntValue(result.get("consultid")));
                    setAttentioned(Utils.getBooleanValue(result.get("gzflag")));
                }
                LoadingDialog.getInstance(getContext()).dismiss();
            }
        }).start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == HomeActivity.REQUEST_DIALOGTIP) {
                HomeActivity.popFragment();
                AdviceInfo adviceInfo = new AdviceInfo(getContext(), getRealName(), "专家", false);
                adviceInfo.setExpertId(uid);
                CommonFragment fragment = new CommonFragment();
                fragment.setDataModel(adviceInfo);
                fragment.setVariableId(BR.adviceInfo);
                fragment.setLayoutId(R.layout.f_send_advice);
                fragment.setInitCallback(new CommonFragment.Callback() {
                    @Override
                    public void viewCreated(View root) {
                        EditText text = (EditText) root.findViewById(R.id.advice_content);
                        text.requestFocus();
                    }
                });
                HomeActivity.switchFragment((Activity) adviceInfo.getContext(), fragment);
//                HomeActivity.showPersonalCentral(getContext(), uid);
            }
        }
    }
}
